package com.jh.fcsm.controller.sys;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import com.jh.fcsm.common.JarHashCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.sys.VersionHash;
import com.jh.fcsm.beans.sys.vo.VersionHashVo;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.service.sys.VersionHashService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 【请填写功能名称】Controller
 * 
 * @author linq
 * @date 2024-06-03
 */
@RestController
@RequestMapping("/version/hash")
@Api(value="【请填写功能名称】", description="【请填写功能名称】")
public class VersionHashController extends BaseController{
    @Autowired
    private VersionHashService versionHashService;

	private static final String PER_PREFIX = "btn:fcsm:hash:";
	
	/**
	 * 新增【版本信息】
	 *@param versionHash 【版本信息】数据 json
	 *@return RestApiResponse<?>
	 *@author linq
	 */

	@PostMapping("/save")
	@ApiOperation(value = "新增【请填写功能名称】", notes = "新增【请填写功能名称】")
	@SystemLogAnnotation(type = "【请填写功能名称】",value = "新增【请填写功能名称】")
	public RestApiResponse<?> saveVersionHash(@RequestBody VersionHash versionHash) throws IOException, NoSuchAlgorithmException {

		versionHash.setVersionHash(JarHashCalculator.calculateHash());
		String id = versionHashService.saveOrUpdateVersionHash(versionHash);
		return RestApiResponse.ok(id);
	}

	/**
	 * 查询【获取版本详情】详情
	 *@param
	 *@return RestApiResponse<?>
	 *@author linq
	 */
	@GetMapping("/findByVersionHash")
	@ApiOperation(value = "查询【请填写功能名称】详情", notes = "查询【请填写功能名称】详情")
	public RestApiResponse<?> findByVersionHash() throws IOException, NoSuchAlgorithmException {
		VersionHash  versionHash=versionHashService.findByVersionHash();
		if (versionHash==null){
			VersionHash ver=new VersionHash();
			ver.setVersionNumber("V1.0.0");
			ver.setVersionType(4);
			return RestApiResponse.ok(ver);
		}
		//获取最新Hash值
		String hash=versionHash.getVersionHash();
        //获取jar包版本值
		String jarHash= JarHashCalculator.calculateHash();
		//比较Hash值是否相等
		if(!hash.equals(jarHash))
		{
			versionHash.setVersionType(4);
			return RestApiResponse.ok(versionHash);
		}
		return RestApiResponse.ok(versionHash);
	}
	
	/**
	 * 分页查询【请填写功能名称】
	 *@param versionHashVo 【请填写功能名称】 查询条件
	 *@return RestApiResponse<?>
	 *@author linq
	 */
	@PostMapping("/findPageByQuery")
	@ApiOperation(value = "分页查询版本信息", notes = "分页查询【请填写功能名称】")
	public RestApiResponse<?> findPageByQuery(@RequestBody VersionHashVo versionHashVo) {
		PageInfo<VersionHash>  versionHash=versionHashService.findPageByQuery(versionHashVo);
		return RestApiResponse.ok(versionHash);
	}
}
